##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  figure_viii.R
#  -- file produces the mean bar plots in Figure viii in appendix
#
##########################################################

rm(list=ls())
source('~/Dropbox/Seeing_Spots/replication/pre_data_vagov.R')
                 
video_skipped=va_data$video_skipped_60

ymat=cbind(video_skipped, replay, share, getlink, all_y,time_watched, total_time)   

# outcome matrices 

outcome1=outcome2=matrix(NA,7,4)
colnames(outcome1) = colnames(outcome2) = c('Mean Positive','Mean Negative','Difference','pvalue')  
rownames(outcome1) = rownames(outcome2) = c('Skipped','Replay','Share','Get Link','All','Time Watched','Total Time')                   

# (a) pos v. neg
      
outcome1[,1]=colMeans(ymat[pos_ad,],na.rm=T)
outcome1[,2]=colMeans(ymat[!pos_ad,],na.rm=T)   
outcome1[,3]=outcome1[,1]-outcome1[,2]
pvals=array(1,7)
for(i in 1:7){pvals[i]=t.test(ymat[pos_ad,i],ymat[!pos_ad,i])$p.value}
outcome1[,4]=pvals

# (b) dem v. rep 
      
outcome2[,1]=colMeans(ymat[dem_ad,],na.rm=T)
outcome2[,2]=colMeans(ymat[!dem_ad,],na.rm=T)   
outcome2[,3]=outcome2[,1]-outcome2[,2]
pvals=array(1,7)
for(i in 1:7){pvals[i]=t.test(ymat[dem_ad,i],ymat[!dem_ad,i])$p.value}
outcome2[,4]=pvals  

# (c) dem v. rep v. pos v. neg

outcome3=matrix(NA,7,4)
colnames(outcome3) = c('Mean Positive & McAuliffe','Mean Negative & McAuliffe','Mean Positive & Cuccinelli','Mean Negative & Cuccinelli')  
rownames(outcome3) = c('Skipped','Replay','Share','Get Link','All','Time Watched','Total Time')                   

outcome3[,1]=colMeans(ymat[dem_pos,],na.rm=T)
outcome3[,2]=colMeans(ymat[dem_neg,],na.rm=T)         
outcome3[,3]=colMeans(ymat[rep_pos,],na.rm=T)
outcome3[,4]=colMeans(ymat[rep_neg,],na.rm=T)
           


outcomes = list('pos v. neg'=outcome1, 'dem v. rep'=outcome2, '2-way'=outcome3)

# main plots here 

# bootstrap approach
# MAIN EFFECTS;         
 

set.seed(1005)   
po_no_pr_nr=ovr=pvn=list() 
for(k in 1:5){
	pvn[[k]]=ovr[[k]]=matrix(NA,2000,2)
	po_no_pr_nr[[k]]=matrix(NA,2000,4)
}

ym=as.matrix(cbind(all_y,video_skipped,replay,share,getlink))
#colnames(ymat)=c('all','skip','replay','share','getlink')

for(j in 1:2000){
	inds=sample(1:length(tr),size=length(tr),replace=T)
	omega=tr[inds]   
	ymat=ym[inds,]
	       	
	ind1 = which(str_sub(omega,2,2) == 'P' & str_sub(omega,1,1) == "M")
	ind2 = which(str_sub(omega,2,2) == 'N' & str_sub(omega,1,1) == "M")
	ind3 = which(str_sub(omega,2,2) == 'P' & str_sub(omega,1,1) == "C")
	ind4 = which(str_sub(omega,2,2) == 'N' & str_sub(omega,1,1) == "C")
	
	s1=sort(c(ind1,ind3)) # positive
	s2=sort(c(ind2,ind4)) # negative
	
	s3=sort(c(ind1,ind2)) # dem        
	s4=sort(c(ind3,ind4)) # rep 
	
	for(k in 1:5){
		pvn[[k]][j,] = c(mean(ymat[s1,k],na.rm=T),mean(ymat[s2,k],na.rm=T))
		ovr[[k]][j,] = c(mean(ymat[s3,k],na.rm=T),mean(ymat[s4,k],na.rm=T))  
		po_no_pr_nr[[k]][j,] = c(mean(ymat[ind1,k],na.rm=T),mean(ymat[ind2,k],na.rm=T),
								 mean(ymat[ind3,k],na.rm=T),mean(ymat[ind4,k],na.rm=T))
	}  
}
    
colnames(ymat)=c('all','skip','replay','share','getlink')    
pvn[[1]]=pvn[[1]]/4
ovr[[1]]=ovr[[1]]/4
po_no_pr_nr[[1]]=po_no_pr_nr[[1]]/4
      
  
# pos v. neg for partisans v. independents
partisans=abs(pid_lean)==1

# counting
out=c(all_y/4,video_skipped,replay,share,getlink)
tr_out=rep(pos_ad,5)
x_out=c(rep(0,length(all_y)),rep(1,length(all_y)),rep(2,length(all_y)),rep(3,length(all_y)),rep(4,length(all_y)))
ns=t(c(table(interaction(!tr_out[which(!is.na(out))],x_out[which(!is.na(out))]))))


# pos v. neg
# positive v. negative for all ..... bar figure; bar meanplot below
{    
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/pos_neg_all_60_VA.pdf')    
# skipping the video    
plot(x=c(1,1),y=c(1,1),ylim=c(0,.85),xlim=c(1,10),pch='-',col='white',axes=F,xlab='',ylab='Ad-Seeking Choices',type='b')
abline(v=c(2.5,4.5,6.5,8.5),lty=2)
box()
mtext(text='All Items',side=3,at=1.5,cex=1)
mtext(text='Skip Ad',side=3,at=3.5,cex=1)
mtext(text='Replay',side=3,at=5.5,cex=1)    
mtext(text='Share',side=3,at=7.5,cex=1)    
mtext(text='Get Link',side=3,at=9.5,cex=1)    
axis(2)
axis(1,at=c(1,2,3,4,5,6,7,8,9,10,11,12),labels=F)
text(labels= c(paste("\nPositive\n(n=",ns[1],")",sep='')),x=1.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nNegative\n(n=",ns[2],")",sep='')),x=2.1,y=-.055,srt=0,xpd=T,cex=.75)  
text(labels= c(paste("\nPositive\n(n=",ns[3],")",sep='')),x=3.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nNegative\n(n=",ns[4],")",sep='')),x=4.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nPositive\n(n=",ns[5],")",sep='')),x=5.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nNegative\n(n=",ns[6],")",sep='')),x=6.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nPositive\n(n=",ns[7],")",sep='')),x=7.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nNegative\n(n=",ns[8],")",sep='')),x=8.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nPositive\n(n=",ns[9],")",sep='')),x=9.1,y=-.055,srt=0,xpd=T,cex=.75)
text(labels= c(paste("\nNegative\n(n=",ns[10],")",sep='')),x=10.1,y=-.055,srt=0,xpd=T,cex=.75)                                                                  
eps=.0015
cols='black'
cnts=0        
	
for(k in 1:5){
j=1   
cnts=cnts+1
y1=quantile(pvn[[k]][,j],prob=c(.025,.975))
y2=mean(pvn[[k]][,j])  
j=cnts
lines(x=c(j,j),y=c(y1[1]-eps,y1[2]+eps),col=cols,lwd=2.5,type='l')
lines(x=c(j-.15,j+.15),y=c(y1[1]-eps,y1[1]-eps),lwd=2.5,col=cols,type='l')
lines(x=c(j-.15,j+.15),y=c(y1[2]+eps,y1[2]+eps),lwd=2.5,col=cols)
points(x=(j),y=y2,pch=15,cex=2.25,col='white')
text(x=(j),y=y2,label=str_sub(as.character(y2),1,4),pch=19,cex=.75,col=cols)

j=2 
cnts=cnts+1  
y1=quantile(pvn[[k]][,j],prob=c(.025,.975))
y2=mean(pvn[[k]][,j])    
j=cnts
lines(x=c(j,j),y=c(y1[1]-eps,y1[2]+eps),col=cols,lwd=2.5,type='l')
lines(x=c(j-.15,j+.15),y=c(y1[1]-eps,y1[1]-eps),lwd=2.5,col=cols,type='l')
lines(x=c(j-.15,j+.15),y=c(y1[2]+eps,y1[2]+eps),lwd=2.5,col=cols)
points(x=(j),y=y2,pch=15,cex=2.25,col='white')
text(x=(j),y=y2,label=str_sub(as.character(y2),1,4),pch=19,cex=.75,col=cols) 
}
  
}       
dev.off()

# END figure_viii.R